//
//  MainVC.m
//  LessoDemo
//
//  Created by decade on 26/12/2017.
//  Copyright © 2017 Lesso. All rights reserved.
//

#import "MainVC.h"
#import "SubVC.h"
#import "ViewHistoryVC.h"

@interface MainVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataArray;

@end

@implementation MainVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = @[@"Furniture", @"Viewed History"];
    self.title = @"Main Page";
    [self setupTableView];
}

- (void)setupTableView {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    UIButton *headerView = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 60)];
    headerView.backgroundColor = [UIColor grayColor];
    UIButton *img = [[UIButton alloc]initWithFrame:CGRectMake(20, 5, 50, 50)];
    [img addTarget:self action:@selector(logout) forControlEvents:UIControlEventTouchUpInside];
    img.backgroundColor = [UIColor orangeColor];
    img.layer.cornerRadius = 25;
    img.layer.masksToBounds = YES;
    [img setImage:[UIImage imageNamed:@"accountIcon"] forState:UIControlStateNormal];
    [headerView addSubview:img];
    UIButton *name = [[UIButton alloc]initWithFrame:CGRectMake(40, 0, 200, 60)];
    [name addTarget:self action:@selector(logout) forControlEvents:UIControlEventTouchUpInside];
    [name setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [headerView addSubview:name];
    NSDictionary *accountInfo = [[NSUserDefaults standardUserDefaults]objectForKey:CUAccount];
    NSString *nameText = accountInfo[@"account"];
    [name setTitle:nameText forState:UIControlStateNormal];
    [headerView addTarget:self action:@selector(logout) forControlEvents:UIControlEventTouchUpInside];
    self.tableView.tableHeaderView = headerView;
    
    UILabel *footLB = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    footLB.numberOfLines = 0;
    footLB.text = @"1.login zone to click it will show logout alert\n\n2.category view click to push sub category\n\n3.Viewed history view";
    self.tableView.tableFooterView = footLB;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    if (indexPath.row == 0) {
        cell.imageView.image = [UIImage imageNamed:@"1"];
    } else {
        cell.imageView.image = [UIImage imageNamed:@"2"];
    }
    cell.imageView.layer.cornerRadius = 25;
    cell.imageView.layer.masksToBounds = YES;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = self.dataArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        SubVC *vc = [[SubVC alloc]init];
        vc.title = self.dataArray[indexPath.row];
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        NSArray *tmparr = [[NSUserDefaults standardUserDefaults]objectForKey:ViewedHistory];
        ViewHistoryVC *vc = [[ViewHistoryVC alloc]init];
        vc.dataArray = tmparr;
        vc.title = self.dataArray[indexPath.row];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)logout {
    [Util showAlertView:@"Make sure logout?" cancelBlock:nil okBlock:^(UIAlertAction *action) {
        appDelegate.window.rootViewController = appDelegate.loginVC;
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:ISLOGIN];
        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:CUAccount];
    }];
}

@end
